package com.sunnada.edu.business.salaryManage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.business.attendance.bean.AttenRecord;
import com.sunnada.edu.business.attendance.bean.Attendance;
import com.sunnada.edu.business.salaryManage.service.PayrollService;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;

/***
 * explain 工资表
 * 
 * @author zhenglx
 */
@Controller
@RequestMapping("/payrollController")
public class PayrollController extends BasicController {
	@Resource
	PayrollService payrollService;

	/** 工资表首页 */
	@RequestMapping("/payroll")
	public String payroll(HttpServletRequest request) {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "business/salaryManage/payroll";
	}

	/** 工资表查询 */
	@RequestMapping("/payrollSearch")
	public void payrollSearch(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		if(params.get("workerId") != null){
			params.put("workerId", Utils.getIdsWithSingleQuotes(params.get("workerId")));
		}
		
		Page page = new Page();
		page.setRowCount(payrollService.getPayrollCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = payrollService.findPayroll(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 考勤记录修改页面 */
	@RequestMapping("/attenRecordUpdate")
	public String attendanceUpdate(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		return "business/attendance/attendance_update";
	}
	
	/** 跳转发放公司页面 */
	@RequestMapping("/pageEditSalary")
	public String pageEditSalary(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		request.setAttribute("workerId", params.get("workerId"));
		request.setAttribute("beginTime", params.get("beginTime"));
		request.setAttribute("userName", params.get("optionUserName"));
		return "business/salaryManage/edit_salary";
	}
	
	/** 考勤记录保存功能 */
	@RequestMapping("/attenRecordSave")
	public void userSave(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		// 验证用户账号是否重复
		String recId = params.get("recId");
		if (Utils.isNotNull(recId)) {
			msg = payrollService.attendanceUpdate(params);
		} else {
			recId = UUID.randomUUID().toString();
			params.put("recId", recId);
			msg = payrollService.attenRecordAdd(params);
		}
		printInfo(response, msg);
	}

	/** 工资表导出 */
	@RequestMapping("/exportPayroll")
	public void exportPayroll(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, AttenRecord.class, params);
		try {
			payrollService.exportAttendance(params, request, response);
		} catch (Exception e) {
			printInfo(response, "<script>alert('导出失败，请稍后重试！');window.history.go(-1);</script>");
		}
	}
	
	/** 发放工资审核提交 */
	@RequestMapping("/payrollToAudit")
	public void payrollToAudit(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		msg = payrollService.payrollToAudit(params);
		printInfo(response, msg);
	}
	
	/*
	 * 根据申请单ID获取申请单信息
	 */
	@RequestMapping("/getPayrollApplyByRecid")
	public void getPayrollApplyByRecid(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Process.class, params);
		List<?> list = payrollService.getPayrollApplyByRecid(params);
		JSONObject json = new JSONObject();		
		json.put("list", list);
		printInfo(response,json);
	}
	
	/** 申请单对应工人查询 */
	@RequestMapping("/getPayrollApplyWorker")
	public void getPayrollApplyWorker(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		Page page = new Page();
		page.setRowCount(payrollService.getPayrollApplyWorkerCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = payrollService.findPayrollApplyWorker(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}
	
	/**修改员工的应发工资*/
	@RequestMapping("/saveRealPay")
	public void saveRealPay(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		msg = payrollService.saveRealPay(params);		
		printInfo(response,msg);
	}
	
	/**工资发放申请审核*/
	@RequestMapping("/audit")
	public void audit(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		msg = payrollService.audit(params);		
		printInfo(response,msg);
	}

}
